package com.qyhl.module_practice.map.sign;


import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;

public class PracticeSignHomeModel implements PracticeSignHomeContract.PracticeSignHomeModel {
    private PracticeSignHomePresenter mPresenter;

    PracticeSignHomeModel(PracticeSignHomePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }


    @Override
    public void signIn(String actId, String qrCode, String address) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_SIGN_IN)
                .params("actId", actId)
                .params("qrCode", qrCode)
                .params("addr", address)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signError("签到失败，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult= new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.signInSuccess();
                        } else {
                            mPresenter.signError(apiResult.getMessage()+"");
                        }
                    }
                });
    }

    @Override
    public void signOut(String actId, String qrCode, String address) {
        EasyHttp.post(PracticeUrl.POST_PRACTICE_SIGN_OUT)
                .params("actId", actId)
                .params("qrCode", qrCode)
                .params("addr", address)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signError("签退失败，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult= new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.signOutSuccess();
                        } else {
                            mPresenter.signError(apiResult.getMessage()+"");
                        }
                    }
                });

    }
}
